package net.ywd.xp1024search.schedule;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.java.Log;
import net.ywd.xp1024search.entity.UsdtCny;
import net.ywd.xp1024search.entity.UsdtCnyResult;
import net.ywd.xp1024search.mapper.UsdtCnyRepository;
import net.ywd.xp1024search.util.WebContentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Random;

@Component
@Log
public class GateIoSchedule {


    @Value("${env}")
    private String env;
    @Value("${gateio.url}")
    private String gateioUrl;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private UsdtCnyRepository usdtCnyRepository;

    @Scheduled(fixedRate = 600000)
    public void usdtCnyPrice() throws IOException, SQLException {
        Random r = new Random();
        int c = r.nextInt(999999);
        String result = WebContentUtil.readWebContent(gateioUrl + c, env);
        UsdtCnyResult usdtCnyResult = objectMapper.readValue(result, UsdtCnyResult.class);
        usdtCnyRepository.save(UsdtCny.convert(usdtCnyResult));
    }
}
